﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using iBing.Common.DbHelper;
using iBing.Common.DbHelper.CoreObj;
using CodeCreator.Template;

namespace CodeCreator.DbHelper
{
    public class DbHelperForSqlServer : IDbHelper
    {
        private string connectionString = "";
        public DbHelperForSqlServer(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public int ExecuteSql(string SQLString)
        {
            using (connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.OracleClient.OracleException E)
                    {
                        connection.Close();
                        throw new Exception(E.Message);
                    }
                }
            }
        }

        public object GetSingle(string SQLString)
        {
            using (connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        object obj = cmd.ExecuteScalar();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.OracleClient.OracleException e)
                    {
                        connection.Close();
                        throw new Exception(e.Message);
                    }
                }
            }
        }

        public IDataReader ExecuteReader(string strSQL)
        {
            connection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(strSQL, connection);
            try
            {
                connection.Open();
                IDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch (System.Data.OracleClient.OracleException e)
            {
                throw new Exception(e.Message);
            }

        }

        public DataSet Query(string SQLString)
        {
            using (connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.OracleClient.OracleException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }

        SqlConnection connection = null;
        public void CloseConnection()
        {
            try
            {
                if (this.connection != null && this.connection.State == ConnectionState.Open)
                {
                    this.connection.Close();
                }
            }
            catch
            { }
        }


        public List<string> GetAllSchema()
        {
            throw new NotImplementedException();
        }

        public List<string> GetTablesBySchema(string schemaName)
        {
            throw new NotImplementedException();
        }

        public List<DbField> GetColumnsByTable(string schemaName, string tableName)
        {
            throw new NotImplementedException();
        }
    }
}
